package com.emm.yixun.estate.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.emm.yixun.basic.service.BaseCodeValueService;
import com.emm.yixun.basic.service.ProjectBuildingService;
import com.emm.yixun.basic.service.ProjectPremisesService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.common.exception.BusinessException;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.ProjectBuilding;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.dto.ProjectBuildingDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.estate.service.HotfixServiceFacade;
import com.emm.yixun.estate.service.ImportExcelServiceFacade;
@Service("hotfixServiceFacade")
@WebService(name="com.emm.yixun.estate.service.HotfixServiceFacade")
public class HotfixServiceFacadeImpl implements HotfixServiceFacade {

    @Autowired
    private ProjectBuildingService projectBuildingService;
    
    @Autowired
    private ProjectPremisesService projectPremisesService;
    
    @Autowired
    private ProjectRoomService projectRoomService;
    
    @Autowired
    private BaseCodeValueService baseCodeValueService;
    
    @Autowired
    private ImportExcelServiceFacade importExcelServiceFacade;
   
    
   
    
    @Override
    public String hotfixIndust(Long projectId) {
	StringBuffer sb = new StringBuffer();
	//Map<String, String> map key= 组团名称-楼栋名称  ， value = 楼栋业态
	Map<String, String> map = new HashMap<String, String>();
	try {
	    
	    ProjectPremisesDto projectPremisesDto = new ProjectPremisesDto(); 
	    projectPremisesDto.setProjectId(projectId);
	    projectPremisesDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
	    List<ProjectPremises> projectPremises =  projectPremisesService.selectListByDto(projectPremisesDto);
	    if(projectPremises!=null&&projectPremises.size()>0){
		for (ProjectPremises premises:projectPremises) {
		    ProjectBuildingDto buildingDto = new ProjectBuildingDto();
		    buildingDto.setProjectId(projectId);
		    buildingDto.setPremisesId(premises.getId());
		    buildingDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		    List<ProjectBuilding> buildingList = projectBuildingService.selectListByDto(buildingDto);
		    if(buildingList!=null&&buildingList.size()>0){
			for (ProjectBuilding building : buildingList) {
			    String key = premises.getGroupName()+"-"+building.getBuildingName();
			    Map<String, String> result = vaild(projectId, premises.getId(), building.getId());
			    if(!StringUtils.isEmpty(result.get("error"))){
				sb.append(result.get("error")+"</br>");
			    }else{ 
				Long industMarkId = new Long(result.get("success"));
				BaseCodeValue baseCodeValue = baseCodeValueService.selectById(industMarkId);
				if(baseCodeValue==null){
				    sb.append("项目ID:"+projectId+",组团ID："+premises.getId()+",楼栋ID:"+building.getId()+"下，房间的业态ID:"+industMarkId+"不存在</br>");
				}else{
				    map.put(key, baseCodeValue.getCodeName());
				}
			    }
			}
		    }
		
		}
	    }
	    if(map!=null&&map.size()>0){
		importExcelServiceFacade.industMarkSummary(map, projectId, null, null);
	    }
	} catch(BusinessException e1){
	    return e1.getErrorMsg();
	}catch (Exception e) {
	   return "系统异常";
	}
	return sb.toString();
    }
    
    /**
     * 
    *
    * @Title: vaild 
    * @Description: 查询项目，组团，楼栋下面的房间，或者车位，业态是否统一
    * @param @param projectId
    * @param @param premises
    * @param @param building
    * @param @return    设定文件 
    * @return Map<String,String>    返回类型 
    * @throws
     */
    public Map<String, String> vaild(Long projectId,Long premises,Long building){
	Map<String, String> map = new HashMap<String, String>();
	ProjectRoomDto roomdto = new ProjectRoomDto();
	roomdto.setProjectId(projectId);
	roomdto.setPremisesId(premises);
	roomdto.setBuildingId(building);
	List<ProjectRoom> projectRoomList = projectRoomService.queryRoomIndustGroup(roomdto);
	if(projectRoomList!=null&&projectRoomList.size()>1){
	    map.put("error", "项目ID:"+projectId+",组团ID:"+premises+",楼栋ID:"+building+"业态不统一,请处理");
	}else{
	    if(projectRoomList.size()>0){  //大于0 ，不大于1 
		
		ProjectRoom roomDto = projectRoomList.get(0);
		if(roomDto==null||StringUtils.isEmpty(roomDto.getIndustMark())){
		    map.put("error","项目ID:"+projectId+",组团ID:"+premises+",楼栋ID:"+building+"业态为空,请处理");
		}else{
		    map.put("success", roomDto.getIndustMark());
		}
		
	    }
	}
	return map;
    }

}
